﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PingTest.Dto;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Net;
using PingTest.Service.Interfaces;

namespace PingTest.Service
{
    public class SiteHttpRequest : ISitePingTest
    {
        object objectLock = new object();
        public string PingSite(SiteInfo siteInfo)
        {
            try
            {

                if (Regex.IsMatch(siteInfo.SiteUrl, @"http(s?)://www\.\S+\.\S+", RegexOptions.Singleline | RegexOptions.IgnoreCase))
                {
                    Console.WriteLine("Pinging:" + siteInfo.SiteUrl);
                    var objRequest = (HttpWebRequest)WebRequest.Create(siteInfo.SiteUrl);
                    // objRequest.Timeout = 3000;
                    var objResponse = (HttpWebResponse)objRequest.GetResponse();
                    Console.WriteLine("Ping Response:" + siteInfo.SiteUrl + ":" + objResponse.StatusCode);

                    return String.Concat("#", ReturnStatusColour(objResponse));
                }
                else
                {
                    return "#ff00ff";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return "#ff00ff";
            }
        }

        private string ReturnStatusColour(HttpWebResponse objResponse)
        {
            switch (objResponse.StatusCode)
            {
                case HttpStatusCode.Accepted:
                    break;
                case HttpStatusCode.Ambiguous:
                    break;
                case HttpStatusCode.BadGateway:
                    break;
                case HttpStatusCode.BadRequest:
                    break;
                case HttpStatusCode.Conflict:
                    break;
                case HttpStatusCode.Continue:
                    break;
                case HttpStatusCode.Created:
                    break;
                case HttpStatusCode.ExpectationFailed:
                    break;
                case HttpStatusCode.Forbidden:
                    return "999";
                case HttpStatusCode.Found:
                    break;
                case HttpStatusCode.GatewayTimeout:
                    break;
                case HttpStatusCode.Gone:
                    break;
                case HttpStatusCode.HttpVersionNotSupported:
                    break;
                case HttpStatusCode.InternalServerError:
                    return "FFFF00";
                case HttpStatusCode.LengthRequired:
                    break;
                case HttpStatusCode.MethodNotAllowed:
                    break;
                case HttpStatusCode.Moved:
                    break;
                //case HttpStatusCode.MovedPermanently:
                //    break;
                //case HttpStatusCode.MultipleChoices:
                //    break;
                case HttpStatusCode.NoContent:
                    break;
                case HttpStatusCode.NonAuthoritativeInformation:
                    break;
                case HttpStatusCode.NotAcceptable:
                    break;
                case HttpStatusCode.NotFound:
                    return "FF0000";
                case HttpStatusCode.NotImplemented:
                    break;
                case HttpStatusCode.NotModified:
                    break;
                case HttpStatusCode.OK:
                    return "40c88b";
                case HttpStatusCode.PartialContent:
                    break;
                case HttpStatusCode.PaymentRequired:
                    break;
                case HttpStatusCode.PreconditionFailed:
                    break;
                case HttpStatusCode.ProxyAuthenticationRequired:
                    break;
                //case HttpStatusCode.Redirect:
                //    break;
                case HttpStatusCode.RedirectKeepVerb:
                    break;
                case HttpStatusCode.RedirectMethod:
                    break;
                case HttpStatusCode.RequestEntityTooLarge:
                    break;
                case HttpStatusCode.RequestTimeout:
                    break;
                case HttpStatusCode.RequestUriTooLong:
                    break;
                case HttpStatusCode.RequestedRangeNotSatisfiable:
                    break;
                case HttpStatusCode.ResetContent:
                    break;
                //case HttpStatusCode.SeeOther:
                //    break;
                case HttpStatusCode.ServiceUnavailable:
                    break;
                case HttpStatusCode.SwitchingProtocols:
                    break;
                //case HttpStatusCode.TemporaryRedirect:
                //    break;
                case HttpStatusCode.Unauthorized:
                    break;
                case HttpStatusCode.UnsupportedMediaType:
                    break;
                case HttpStatusCode.Unused:
                    break;
                case HttpStatusCode.UseProxy:
                    break;
                default:

                    break;
            }
            return "ff00ff";
        }
    }
}
